<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>JS判断一个对象是否为空 | Jayken (谢小舜)|谢小舜的个人博客</title>
    <meta name="description" content=" Jayken（谢小舜）的个人主页，记录我的程序员职业成长之路，分享前端、Java踩过的坑！">
    
    
    <link rel="preload" href="/assets/css/0.styles.0c98b5d4.css" as="style"><link rel="preload" href="/assets/js/app.2d175a42.js" as="script"><link rel="preload" href="/assets/js/2.af4947b8.js" as="script"><link rel="preload" href="/assets/js/8.cfbdc4b7.js" as="script"><link rel="prefetch" href="/assets/js/10.ead388bb.js"><link rel="prefetch" href="/assets/js/11.c26367e4.js"><link rel="prefetch" href="/assets/js/12.e15eef2b.js"><link rel="prefetch" href="/assets/js/13.71248d26.js"><link rel="prefetch" href="/assets/js/14.caa891a6.js"><link rel="prefetch" href="/assets/js/15.dba29244.js"><link rel="prefetch" href="/assets/js/3.8c0cb32d.js"><link rel="prefetch" href="/assets/js/4.ea6b297e.js"><link rel="prefetch" href="/assets/js/5.e528d392.js"><link rel="prefetch" href="/assets/js/6.d3c0fe75.js"><link rel="prefetch" href="/assets/js/7.f9e4b783.js"><link rel="prefetch" href="/assets/js/9.e68d7ec2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0c98b5d4.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar"><header class="navbar"><div class="navbar-wrap"><div><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/logo_z.gif" alt="Jayken (谢小舜)|谢小舜的个人博客" class="logo"> <!----></a> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/post/" class="nav-link router-link-active">博客</a></div><div class="nav-item"><a href="https://github.com/jaykenxie" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/post/" class="nav-link router-link-active">博客</a></div><div class="nav-item"><a href="https://github.com/jaykenxie" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <!----> </aside> <main class="page"><div class="page-left"><div class="page-content"><div class="page-title"><h3>JS判断一个对象是否为空</h3> <div class="page-title_info"><span class="page-title_author"><img src="https://avatars3.githubusercontent.com/u/29194135?v=4" alt class="page-title_avatar"> <span>谢小舜</span></span> <span class="page-title_time"><span>2019-07-10 07:28:13</span></span></div></div> <div class="theme-content content__default"><p>在JS开发中经常会需要判断一个对象是否为空，下面记录了几种简单的方法
</p> <h2 id="用for-in循环"><a href="#用for-in循环" aria-hidden="true" class="header-anchor">#</a> 用for..in循环</h2> <p>用for in循环对象，如果进入循环返回false没进入循环就返回true</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token parameter">obj</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">const</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// 判断自身的属性</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>obj<span class="token punctuation">.</span><span class="token function">hasOwnProperty</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token boolean">false</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="用es6的object-keys"><a href="#用es6的object-keys" aria-hidden="true" class="header-anchor">#</a> 用ES6的Object.keys()</h2> <p>用ES6的Object.keys()会返回对象的所有key组成的数组，再通过判断keys是否为空来判断对象是否为空</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token parameter">obj</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> keys <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
  <span class="token comment">// 判断数组是否为空</span>
  <span class="token keyword">return</span> keys <span class="token operator">==</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="转换成json判断"><a href="#转换成json判断" aria-hidden="true" class="header-anchor">#</a> 转换成JSON判断</h2> <p>用JSON.stringify转成json字符串来判断</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token parameter">obj</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">'{}'</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></div> <div class="page-foot"><span><a href="/post/#JS">#JS</a></span></div></div> <div class="page-comment"><!----></div> <!----> </div> <div class="page-right"><div class="toc"><h4>目录</h4> <ul class="toc-wrap"><!----> <ul><li><a href="#用for-in循环">用for..in循环</a></li></ul> <!----> <!----><!----> <ul><li><a href="#用es6的object-keys">用ES6的Object.keys()</a></li></ul> <!----> <!----><!----> <ul><li><a href="#转换成json判断">转换成JSON判断</a></li></ul> <!----> <!----></ul></div></div></main> <footer class="footer" data-v-437632ea><div class="footer-ctx" data-v-437632ea><div class="footer-ctx-l" data-v-437632ea><h5 data-v-437632ea>自述</h5> <p data-v-437632ea>接触前端以来一直想自己设计开发一个博客，用于分想和记录工作、生活和学习中遇到的问题，与解决问题的思路和感悟。 (^o^)/~。</p></div> <div class="footer-ctx-m" data-v-437632ea><h5 data-v-437632ea>相关链接</h5> <ul class="flink" data-v-437632ea><li class="flink-wrap" data-v-437632ea><a href="/" class="dedline" data-v-437632ea>关于我</a></li><li class="flink-wrap" data-v-437632ea><a href="https://github.com/jaykenxie" class="dedline" data-v-437632ea>GitHub</a></li><li class="flink-wrap" data-v-437632ea><a href="https://weibo.com/autoshun" class="dedline" data-v-437632ea>微博</a></li></ul></div> <div class="footer-ctx-r" data-v-437632ea><img src="/images/logo_w.png" alt="Jayken (谢小舜)|谢小舜的个人博客" class="footer-ctx-r_logo" data-v-437632ea></div></div> <div class="footer-info" data-v-437632ea><a href="/" data-v-437632ea>©Jayken</a> <i data-v-437632ea> · </i> <a href="https://v1.vuepress.vuejs.org" data-v-437632ea>Vuepress</a>
      |  
    <a href="http://www.miitbeian.gov.cn/publish/query/indexFirst.action" target="_blank" class="dedline" data-v-437632ea>粤ICP备18012800号-2</a></div></footer></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.2d175a42.js" defer></script><script src="/assets/js/2.af4947b8.js" defer></script><script src="/assets/js/8.cfbdc4b7.js" defer></script>
  </body>
</html>
